//
//  PubFunction.m
//  EnglishDemo
//
//  Created by huanghb on 11-1-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PubFunction.h"
#import <QuartzCore/QuartzCore.h>

@implementation PubFunction


+(NSString *)getRetainDays:(NSString *)oldtime
{
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *  senddate=[NSDate date];
    //结束时间
    NSDate *endDate = [dateFormatter dateFromString:oldtime];
    //当前时间
    NSDate *senderDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:senddate]];
    //得到相差秒数
    NSTimeInterval time=[endDate timeIntervalSinceDate:senderDate];
    
    int days = ((int)time)/(3600*24);

    return [NSString stringWithFormat:@"%d",days];
}

+(NSString *)getRetainTime:(NSString *)oldtime
{
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *  senddate=[NSDate date];
    //结束时间
    NSDate *endDate = [dateFormatter dateFromString:oldtime];
    //当前时间
    NSDate *senderDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:senddate]];
    //得到相差秒数
    NSTimeInterval time=[endDate timeIntervalSinceDate:senderDate];
    
    int days = ((int)time)/(3600*24);
//    int hours = ((int)time)%(3600*24)/3600;
//    int minute = ((int)time)%(3600*24)600/60;
//
    if (days == 0)
    {
        NSString *dateContent=@"1天后结束";
        return dateContent;
    }
    if(days < 0)
    {
        NSString *dateContent=@"已结束";
        return dateContent;
    }
//    else
//        dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时%i分钟",days,hours,minute];
    NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天后结束",days];
    return dateContent;
}

+ (BOOL) stringIsNullOrEmpty: (NSString *) obj{
	if (obj == nil) {
		return YES;
	}
	if (![obj isKindOfClass:[NSString class]]) {
		return YES;
	}
	if ([[obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) {
		return YES;
	}
	return NO;
}

+(NSString *)toJson:(NSMutableDictionary *)dic
{
    BOOL isTurnableToJSON =[NSJSONSerialization isValidJSONObject: dic];
    if(!isTurnableToJSON)
        return nil;
    NSError* error =nil;
    id result =[NSJSONSerialization dataWithJSONObject:dic
                                               options:kNilOptions error:&error];
    NSString *jsonStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
    
    if(error !=nil)return nil;
    return jsonStr;
}


+(NSString *)getYHQFormatTime:(NSTimeInterval)timeInterval
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

+(NSString *)getTimeByFormat:(NSDate *)date format:(NSString *)format
{
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    if(format == nil)
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    else
        [dateFormatter setDateFormat:format];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    //输出格式为：2010-10-27 10:22:13
    //alloc后对不使用的对象别忘了release
    return currentDateStr;

}

+(UIImage *)GetSkinImage:(NSString *)imagename
{
    NSString *skin = [[NSUserDefaults standardUserDefaults] objectForKey:@"skin"];
    NSString *houzhui = @"skin1";
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@",imagename,houzhui]];
    return image;
//    NSString *skin = [[NSUserDefaults standardUserDefaults] objectForKey:@"imagename"];
//    if(skin == nil)
//        skin = @"skin1";
//    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@",imagename,skin]];
//    return image;
}

+(UIImage *)GetImage:(NSString *)imagename
{
    NSString *skin = [[NSUserDefaults standardUserDefaults] objectForKey:@"skin"];
    if([skin isEqualToString:@"1"])
    {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_skin1",imagename]];
        return image;
    }
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_skin2",imagename]];
    return image;
}

+(UIImage *)SelectImage:(NSString *)imagename
{
    NSString *skin = [[NSUserDefaults standardUserDefaults] objectForKey:@"skin"];
    NSString *houzhui = @"skin1";
    if([skin isEqualToString:@"2"])
        houzhui = @"skin2";
    if([skin isEqualToString:@"3"])
        houzhui = @"skin3";
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@",imagename,houzhui]];
    return image;
}
@end
